package com.jkl.service.home;

import com.jkl.dao.home.MainIndexMapper;
import com.jkl.entity.home.HomeMappingEntity;
import com.jkl.entity.home.HomeRequestEntity;
import com.jkl.entity.home.HomeResponseEntity;
import com.jkl.service.home.mainIndexInter.MainIndexService;
import com.jkl.untils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zjj
 * @date 2019/9/2 15:29
 * @Description ${描述一个类的相关信息}
 */
@Service
public class MainIndexServiceImpl implements MainIndexService {

    @Autowired
    private MainIndexMapper mainIndexMapperDao;

    /**
     * 城镇垃圾产生量
     */
    @Override
    public HomeResponseEntity getProductionAmountAnalysis(HomeRequestEntity homeRequestEntity) {
        String startTime = LocalDate.now().minusYears(6).format(DateTimeFormatter.ofPattern("yyyy")) + "-12";
        String endTime = LocalDate.now().minusYears(1).format(DateTimeFormatter.ofPattern("yyyy")) + "-01";
        homeRequestEntity.setEndTime(endTime);
        homeRequestEntity.setStartTime(startTime);
        HomeResponseEntity responseEntity = new HomeResponseEntity();
        List<HomeMappingEntity> garbageTotal = mainIndexMapperDao.getGarbageTotal(homeRequestEntity);
        List<String> listTime = new ArrayList<>();
        List<Integer> listWeight = new ArrayList<>();
        if (!CollectionUtils.isEmpty(garbageTotal)) {
            for (HomeMappingEntity homeMappingEntity : garbageTotal) {
                listTime.add(homeMappingEntity.getYear() != null ? homeMappingEntity.getYear() : "");
                BigDecimal total = new BigDecimal(homeMappingEntity.getGarbageTotal() != null ? homeMappingEntity.getGarbageTotal() : 0);
                listWeight.add(total.divide(new BigDecimal(10000)).intValue());
            }
        }
        responseEntity.setGarbageTotalArray(listWeight);
        responseEntity.setYearArray(listTime);
        return responseEntity;

    }

    /**
     * 城镇垃圾产生量和累计同比增长率
     */
    @Override
    public HomeResponseEntity getCityGarbageAndRate(HomeRequestEntity homeRequestEntity) {
        List<Map<String, Object>> nowTimeList = new ArrayList<>();
        List<String> yearAndMonth = DateUtil.getYearAndMonth(0);
        for (int i = 0; i < yearAndMonth.size(); i++) {
            Map<String, Object> keyMap = new HashMap<>();
            keyMap.put(yearAndMonth.get(i), 0);
            nowTimeList.add(keyMap);
        }
        String startTime = LocalDate.now().getYear() + "-01";//当前年开始时间
        String endTime = LocalDate.now().minusMonths(1).format(DateTimeFormatter.ofPattern("yyyy-MM"));//当前时间的前一个月
        homeRequestEntity.setEndTime(endTime);
        homeRequestEntity.setStartTime(startTime);
        HomeResponseEntity responseEntity = new HomeResponseEntity();
        List<String> listTime = new ArrayList<>();
        List<Integer> listWeight = new ArrayList<>();
        //当前年各个月的城镇垃圾产生量
        List<HomeMappingEntity> garbageTotalVByTime = mainIndexMapperDao.getGarbageTotalVByTime(homeRequestEntity);
        for (Map<String, Object> map : nowTimeList) {
            String key = map.keySet().iterator().next();
            for (HomeMappingEntity homeMappingEntity : garbageTotalVByTime) {
                String time = DateUtil.formatToView(homeMappingEntity.getYear());
                if (time.equals(key)) {
                    map.put(time, homeMappingEntity.getGarbageTotal() != null ? homeMappingEntity.getGarbageTotal() : 0);
                    break;
                }
            }
            listTime.add(key);
            BigDecimal garbageTotal = new BigDecimal(map.get(key).toString());
            listWeight.add(garbageTotal.divide(new BigDecimal(10000), 0, BigDecimal.ROUND_HALF_UP).intValue());
        }

        responseEntity.setGarbageTotalArray(listWeight);
        responseEntity.setYearArray(listTime);
        //同比累计增长率
        List<Integer> growthRateArray = rateArray(nowTimeList, homeRequestEntity);
        responseEntity.setGrowthRateArray(growthRateArray);
        return responseEntity;
    }

    /**
     * 累计同比增长
     */
    private List<Integer> rateArray(List<Map<String, Object>> nowYear0, HomeRequestEntity homeRequestEntity) {

        List<Map<String, Object>> lastYear0 = new ArrayList<>();
        List<String> yearAndMonth = DateUtil.getYearAndMonth(1);
        for (int i = 0; i < yearAndMonth.size(); i++) {
            Map<String, Object> keyMap = new HashMap<>();
            keyMap.put(yearAndMonth.get(i), 0);
            lastYear0.add(keyMap);
        }
        String lastStartTime = LocalDate.now().minusYears(1).format(DateTimeFormatter.ofPattern("yyyy")) + "- 01";//去年此时
        String lastEndTime = LocalDate.now().minusYears(1).minusMonths(1).format(DateTimeFormatter.ofPattern("yyyy-MM"));//去年此时前一个月
        homeRequestEntity.setEndTime(lastEndTime);
        homeRequestEntity.setStartTime(lastStartTime);
        //获取去年各个月的城镇垃圾产生量
        List<HomeMappingEntity> lastYearGarbgae = mainIndexMapperDao.getGarbageTotalVByTime(homeRequestEntity);
        for (Map<String, Object> map : lastYear0) {
            String key = map.keySet().iterator().next();
            for (HomeMappingEntity homeMappingEntity : lastYearGarbgae) {
                String time = DateUtil.formatToView(homeMappingEntity.getYear());
                if (time.equals(key)) {
                    map.put(time, homeMappingEntity.getGarbageTotal());
                    break;
                }
            }

        }
        List<HomeMappingEntity> nowYear = new ArrayList<>();
        List<HomeMappingEntity> lastYear = new ArrayList<>();
        nowYear0.forEach(m1 -> {
            HomeMappingEntity s = new HomeMappingEntity();
            s.setYear(m1.keySet().iterator().next());
            s.setGarbageTotal((Double.parseDouble(m1.get(s.getYear()).toString())));
            nowYear.add(s);
        });

        lastYear0.forEach(m1 -> {
            HomeMappingEntity s = new HomeMappingEntity();
            s.setYear(m1.keySet().iterator().next());
            s.setGarbageTotal((Double.parseDouble(m1.get(s.getYear()).toString())));
            lastYear.add(s);
        });

        List<Double> nowYearTotal = new ArrayList<>();
        nowYear.forEach(g -> {
            if (nowYearTotal.isEmpty()) {
                nowYearTotal.add(g.getGarbageTotal());
            } else {
                nowYearTotal.add(nowYearTotal.get(nowYearTotal.size() - 1) + g.getGarbageTotal());
            }
        });
        List<Double> lastYearTotal = new ArrayList<>();
        lastYear.forEach(g -> {
            if (lastYearTotal.isEmpty()) {
                lastYearTotal.add(g.getGarbageTotal());
            } else {
                lastYearTotal.add(lastYearTotal.get(lastYearTotal.size() - 1) + g.getGarbageTotal());
            }
        });
        List<Integer> rateArray = new ArrayList<>();

        for (int i = 0; i < nowYear.size(); i++) {
            BigDecimal nowTotal = new BigDecimal(nowYearTotal.get(i));
            BigDecimal lastTotal;
            if (lastYearTotal.get(i) == 0) {
                rateArray.add(0);//计算增长率
            } else {
                lastTotal = new BigDecimal(lastYearTotal.get(i));
                int value = ((nowTotal.divide(lastTotal, 0, BigDecimal.ROUND_HALF_UP)).subtract(new BigDecimal(1))).intValue();//计算增长率
                rateArray.add(value);
            }
        }
        return rateArray;
    }

    /**
     * 无害化处理率
     */
    @Override
    public HomeResponseEntity getHarmLessRateAnalysis(HomeRequestEntity homeRequestEntity) {
        // 获取最近一年的起始和结束时间，本月不算
        String startTime = LocalDate.now().minusYears(1).format(DateTimeFormatter.ofPattern("yyyy-MM"));
        String endTime = LocalDate.now().minusMonths(1).format(DateTimeFormatter.ofPattern("yyyy-MM"));
        homeRequestEntity.setStartTime(startTime);
        homeRequestEntity.setEndTime(endTime);
        List<HomeMappingEntity> homeMappingEntities = mainIndexMapperDao.harmRate(homeRequestEntity);
        //获取最近一年月份列表
        List<String> montListByTime = DateUtil.getMontListByTime(startTime, endTime);
        List<Map<String, Object>> monthlistMap = new ArrayList<>();
        for (int i = 0; i < montListByTime.size(); i++) {
            Map mapTime = new HashMap();
            mapTime.put(DateUtil.formatToView(montListByTime.get(i)), 0);
            monthlistMap.add(mapTime);
        }
        List<Integer> rate = new ArrayList<>();
        List<String> yearArray = new ArrayList<>();
        for (Map<String, Object> map : monthlistMap) {
            String key = map.keySet().iterator().next();
            for (HomeMappingEntity homeMappingEntity : homeMappingEntities) {
                String time = DateUtil.formatToView(homeMappingEntity.getYear());
                if (time.equals(key)) {
                    //计算无害化处理率
                    int value = 0;
                    BigDecimal deviceTotal = new BigDecimal(homeMappingEntity.getDeviceTotal() != null ? homeMappingEntity.getDeviceTotal() : 0);
                    BigDecimal recycleTotal = new BigDecimal(homeMappingEntity.getRecycleTotal() != null ? homeMappingEntity.getRecycleTotal() : 0);
                    if (homeMappingEntity.getPersonGarbageTotal() != null && homeMappingEntity.getPersonGarbageTotal().intValue() != 0) {
                        BigDecimal personGarbageTotal = new BigDecimal(homeMappingEntity.getPersonGarbageTotal());
                        value = (deviceTotal.divide(personGarbageTotal.subtract(recycleTotal), 2, BigDecimal.ROUND_HALF_DOWN)).multiply(new BigDecimal(100)).intValue();
                    }
                    map.put(key, value);
                    break;
                }
            }
            yearArray.add(key);
            rate.add(Integer.parseInt(map.get(key).toString()));
        }
        HomeResponseEntity homeResponseEntity = new HomeResponseEntity();
        homeResponseEntity.setHarmLessRateArray(rate);
        homeResponseEntity.setYearArray(yearArray);
        return homeResponseEntity;
    }

    /**
     * 近一年各个垃圾产生量和占比
     */
    @Override
    public HomeResponseEntity getHarmLesscircleAnalysis(HomeRequestEntity homeRequestEntity) {
        String startTime = LocalDate.now().minusYears(1).format(DateTimeFormatter.ofPattern("yyyy-MM"));
        String endTime = LocalDate.now().minusMonths(1).format(DateTimeFormatter.ofPattern("yyyy-MM"));
        homeRequestEntity.setStartTime(startTime);
        homeRequestEntity.setEndTime(endTime);
        HomeResponseEntity responseEntity = new HomeResponseEntity();
        //内圈指标
        HomeMappingEntity harmLessInnercircleAnalysis = mainIndexMapperDao.getHarmLessInnercircleAnalysis(homeRequestEntity);
        if (harmLessInnercircleAnalysis != null && harmLessInnercircleAnalysis.getTotal() != null && harmLessInnercircleAnalysis.getTotal().intValue() != 0) {
            responseEntity.setLandfill(new BigDecimal(harmLessInnercircleAnalysis.getLandfill() != null ? harmLessInnercircleAnalysis.getLandfill() : 0).setScale(0,BigDecimal.ROUND_HALF_UP).intValue());
            responseEntity.setIncineration(new BigDecimal(harmLessInnercircleAnalysis.getIncineration() != null ? harmLessInnercircleAnalysis.getIncineration() : 0).setScale(0,BigDecimal.ROUND_HALF_UP).intValue());
            responseEntity.setDisposed(new BigDecimal(harmLessInnercircleAnalysis.getDisposed() != null ? harmLessInnercircleAnalysis.getDisposed() : 0).setScale(0,BigDecimal.ROUND_HALF_UP).intValue());
            responseEntity.setKitchen(new BigDecimal(harmLessInnercircleAnalysis.getKitchen() != null ? harmLessInnercircleAnalysis.getKitchen() : 0).setScale(0,BigDecimal.ROUND_HALF_UP).intValue());
        } else {
            responseEntity.setLandfillRadio(0);
            responseEntity.setKitchenRadio(0);
            responseEntity.setDisposedRadio(0);
            responseEntity.setIncinerationRadio(0);
        }
        //外圈指标
        HomeMappingEntity harmLessOutcircleAnalysis = mainIndexMapperDao.getHarmLesscircleAnalysis(homeRequestEntity);
        if (harmLessOutcircleAnalysis != null && harmLessOutcircleAnalysis.getTotal()!=null && harmLessOutcircleAnalysis.getTotal().intValue()!=0) {
            responseEntity.setPerishableGarbageRadio(new BigDecimal(harmLessOutcircleAnalysis.getPerishableGarbageRadio() != null ? harmLessOutcircleAnalysis.getPerishableGarbageRadio() : 0).divide(new BigDecimal(harmLessOutcircleAnalysis.getTotal()), 2, BigDecimal.ROUND_HALF_DOWN).multiply(new BigDecimal(100)).intValue());
            responseEntity.setHarmGarbageRadio(new BigDecimal(harmLessOutcircleAnalysis.getHarmGarbageRadio()!=null?harmLessOutcircleAnalysis.getHarmGarbageRadio():0).divide(new BigDecimal(harmLessOutcircleAnalysis.getTotal()), 2, BigDecimal.ROUND_HALF_DOWN).multiply(new BigDecimal(100)).intValue());
            responseEntity.setRecleGarbageRadio(new BigDecimal(harmLessOutcircleAnalysis.getRecleGarbageRadio() != null ? harmLessOutcircleAnalysis.getRecleGarbageRadio() : 0).divide(new BigDecimal(harmLessOutcircleAnalysis.getTotal()), 2, BigDecimal.ROUND_HALF_DOWN).multiply(new BigDecimal(100)).intValue());
            responseEntity.setOtherGarbageRadio(new BigDecimal(harmLessOutcircleAnalysis.getOtherGarbageRadio() != null ? harmLessOutcircleAnalysis.getOtherGarbageRadio() : 0).divide(new BigDecimal(harmLessOutcircleAnalysis.getTotal()), 2, BigDecimal.ROUND_HALF_DOWN).multiply(new BigDecimal(100)).intValue());

        } else {
            responseEntity.setPerishableGarbageRadio(0);
            responseEntity.setHarmGarbageRadio(0);
            responseEntity.setRecleGarbageRadio(0);
            responseEntity.setOtherGarbageRadio(0);
        }
        responseEntity.setStarttime(startTime);
        responseEntity.setEndtime(endTime);
        return responseEntity;
    }

    /**
     * 资源化分析柱状图
     */
    @Override
    public HomeResponseEntity getResourceAnalysis(HomeRequestEntity homeRequestEntity) {
        //获取最近一年开始时间和结束时间不包括本月
        String startTime = LocalDate.now().minusYears(1).format(DateTimeFormatter.ofPattern("yyyy-MM"));
        String endTime = LocalDate.now().minusMonths(1).format(DateTimeFormatter.ofPattern("yyyy-MM"));
        //获取最近一年月份列表
        List<String> montListByTime = DateUtil.getMontListByTime(startTime, endTime);
        //获取最近两年开始时间和结束时间不包括本月
        String startTimeBefore = LocalDate.now().minusYears(2).format(DateTimeFormatter.ofPattern("yyyy-MM"));
        String endTimeBefore = LocalDate.now().minusYears(1).minusMonths(1).format(DateTimeFormatter.ofPattern("yyyy-MM"));
        List<String> lastMonthListByTime = DateUtil.getMontListByTime(startTimeBefore, endTimeBefore);

        homeRequestEntity.setStartTime(startTime);
        homeRequestEntity.setEndTime(endTime);
        List<HomeMappingEntity> resourceAnalysis = mainIndexMapperDao.getResourceAnalysis(homeRequestEntity);
        List<HomeMappingEntity> resourceAnalysisLastYear = beforeYearCount(homeRequestEntity);

        HomeResponseEntity responseEntity = new HomeResponseEntity();
        List<Map<String, Object>> monthlistMap = new ArrayList<>();
        List<Map<String, Object>> lastYearTimeList = new ArrayList<>();
        for (int i = 0; i < montListByTime.size(); i++) {
            Map mapTime = new HashMap();
            mapTime.put(DateUtil.formatToView(montListByTime.get(i)), 0);
            monthlistMap.add(mapTime);
        }
        for (int i = 0; i < lastMonthListByTime.size(); i++) {
            Map mapTime = new HashMap();
            mapTime.put(DateUtil.formatToView(lastMonthListByTime.get(i)), 0);
            lastYearTimeList.add(mapTime);
        }
        List<String> yearArray = new ArrayList<>();
        List<Integer> kitchenTotalArray = new ArrayList<>();
        List<Integer> incinerationTotalArray = new ArrayList<>();
        List<Integer> disposedTotalArray = new ArrayList<>();
        boolean a = false;
        for (Map<String, Object> map : monthlistMap) {
            String key = map.keySet().iterator().next();
            for (HomeMappingEntity analysis : resourceAnalysis) {
                String time = DateUtil.formatToView(analysis.getYear());
                if (key.equals(time)) {
                    int kitchen = new BigDecimal(analysis.getKitchenTotal() != null ? analysis.getKitchenTotal() : 0).divide(new BigDecimal(10000), 0, BigDecimal.ROUND_HALF_UP).intValue();
                    int incineration = new BigDecimal(analysis.getIncinerationTotal() != null ? analysis.getIncinerationTotal() : 0).divide(new BigDecimal(10000), 0, BigDecimal.ROUND_HALF_UP).intValue();
                    int disposed = new BigDecimal(analysis.getDisposedTotal() != null ? analysis.getDisposedTotal() : 0).divide(new BigDecimal(10000), 0, BigDecimal.ROUND_HALF_UP).intValue();
                    kitchenTotalArray.add(kitchen);
                    incinerationTotalArray.add(incineration);
                    disposedTotalArray.add(disposed);
                    a = true;
                    break;
                }
            }
            if (!a) {
                kitchenTotalArray.add(0);
                incinerationTotalArray.add(0);
                disposedTotalArray.add(0);
            }
            a=false;
            yearArray.add(key);
        }
        List<Map<String, Object>> nowYear = ResourceRate(resourceAnalysis, monthlistMap);
        List<Map<String, Object>> lastYear = ResourceRate(resourceAnalysisLastYear, lastYearTimeList);
        List<Integer> resourceRateArray = resourceRateMap(nowYear, lastYear);
        responseEntity.setYearArray(yearArray);
        responseEntity.setKitchenTotalArray(kitchenTotalArray);
        responseEntity.setResourceRateArray(resourceRateArray);
        responseEntity.setDisposedTotaArray(disposedTotalArray);
        responseEntity.setIncinerationTotalArray(incinerationTotalArray);
        return responseEntity;
    }

    /**
     * 统计前年同比处置数量
     */
    private List<HomeMappingEntity> beforeYearCount(HomeRequestEntity homeRequestEntity) {
        //获取最近两年开始时间和结束时间不包括本月
        String startTimeBefore = LocalDate.now().minusYears(2).format(DateTimeFormatter.ofPattern("yyyyMM"));
        String endTimeBefore = LocalDate.now().minusYears(1).minusMonths(1).format(DateTimeFormatter.ofPattern("yyyyMM"));
        homeRequestEntity.setEndTime(endTimeBefore);
        homeRequestEntity.setStartTime(startTimeBefore);
        List<HomeMappingEntity> resourceAnalysisLastYear = mainIndexMapperDao.getResourceAnalysis(homeRequestEntity);
        return resourceAnalysisLastYear;
    }

    /**
     * 获取资源化利用率
     */
    private List<Map<String, Object>> ResourceRate(List<HomeMappingEntity> list, List<Map<String, Object>> monthlistMap) {

        for (Map<String, Object> map : monthlistMap) {
            for (HomeMappingEntity homeMappingEntity : list) {
                String key = map.keySet().iterator().next();
                String time = DateUtil.formatToView(homeMappingEntity.getYear());
                if (key.equals(time)) {
                    int value = 0;
                    BigDecimal total = new BigDecimal(homeMappingEntity.getTotal() != null ? homeMappingEntity.getTotal() : 0);
                    if (homeMappingEntity.getPersonGarbageTotal() != null && homeMappingEntity.getRecycleTotal().intValue() != 0) {
                        BigDecimal divisor = new BigDecimal(homeMappingEntity.getPersonGarbageTotal()).subtract(new BigDecimal(homeMappingEntity.getRecycleTotal()));
                        value = total.divide(divisor, 0, BigDecimal.ROUND_HALF_UP).intValue();
                    }
                    map.put(key, value);
                    break;
                }
            }
        }
        return monthlistMap;
    }

    /**
     * 计算同比资源增长率
     */
    private List<Integer> resourceRateMap(List<Map<String, Object>> nowYear0, List<Map<String, Object>> lastYear0) {
        List<Integer> nowRateArray = new ArrayList<>();
        List<Integer> lastRateArray = new ArrayList<>();
        List<Integer> rateList = new ArrayList<>();
        for (Map<String, Object> map : nowYear0) {
            String key = map.keySet().iterator().next();
            nowRateArray.add(Integer.parseInt(map.get(key).toString()));
        }
        for (Map<String, Object> map : lastYear0) {
            String key = map.keySet().iterator().next();
            lastRateArray.add(Integer.parseInt(map.get(key).toString()));
        }
        for (int i = 0; i < nowRateArray.size(); i++) {
            BigDecimal nowTotal = new BigDecimal(nowRateArray.get(i));
            BigDecimal lastTotal;
            if (lastRateArray.get(i).equals(0)) {
                rateList.add(0);
            } else {
                lastTotal = new BigDecimal(lastRateArray.get(i));
                int value = ((nowTotal.subtract(lastTotal)).divide(lastTotal, 4, BigDecimal.ROUND_HALF_DOWN)).setScale(2, BigDecimal.ROUND_HALF_DOWN).intValue();//计算增长率
                rateList.add(value);
            }

        }
        return rateList;
    }

    /**
     * 资源化分析饼状图
     */
    @Override
    public HomeResponseEntity getResourceCircleAnalysis(HomeRequestEntity homeRequestEntity) {
        String startTime = LocalDate.now().minusYears(1).format(DateTimeFormatter.ofPattern("yyyy-MM"));
        String endTime = LocalDate.now().minusMonths(1).format(DateTimeFormatter.ofPattern("yyyy-MM"));
        homeRequestEntity.setStartTime(startTime);
        homeRequestEntity.setEndTime(endTime);
        HomeMappingEntity resourceCircleAnalysis = mainIndexMapperDao.getResourceCircleAnalysis(homeRequestEntity);
        if (resourceCircleAnalysis != null) {
            HomeResponseEntity responseEntity = new HomeResponseEntity();
            int disposedTotal = new BigDecimal(resourceCircleAnalysis.getDisposedTotal() != null ? resourceCircleAnalysis.getDisposedTotal() : 0).divide(new BigDecimal(10000), 0, BigDecimal.ROUND_HALF_UP).intValue();
            int kitchenTotal = new BigDecimal(resourceCircleAnalysis.getKitchenTotal() != null ? resourceCircleAnalysis.getKitchenTotal() : 0).divide(new BigDecimal(10000), 0, BigDecimal.ROUND_HALF_UP).intValue();
            int incinerationTotal = new BigDecimal(resourceCircleAnalysis.getIncinerationTotal() != null ? resourceCircleAnalysis.getIncinerationTotal() : 0).divide(new BigDecimal(10000), 0, BigDecimal.ROUND_HALF_UP).intValue();
            responseEntity.setDisposedTotal(disposedTotal);
            responseEntity.setKitchenTotal(kitchenTotal);
            responseEntity.setIncinerationTotal(incinerationTotal);
            responseEntity.setStarttime(startTime);
            responseEntity.setEndtime(endTime);

            return responseEntity;
        } else {
            HomeResponseEntity responseEntity = new HomeResponseEntity();
            responseEntity.setDisposedTotal(0);
            responseEntity.setKitchenTotal(0);
            responseEntity.setIncinerationTotal(0);
            responseEntity.setStarttime(startTime);
            responseEntity.setEndtime(endTime);
            return responseEntity;
        }
    }

    /**
     * 设施概况
     */
    @Override
    public Map<String, Object> getDeviceCount(HomeRequestEntity homeRequestEntity) {
        Map<String, Object> resultMap = new HashMap<>();
        String endTime = LocalDate.now().plusYears(1).format(DateTimeFormatter.ofPattern("yyyy"));
        String starTime = LocalDate.now().minusYears(1).format(DateTimeFormatter.ofPattern("yyyy"));
        homeRequestEntity.setStartTime(starTime);
        homeRequestEntity.setEndTime(endTime);
        homeRequestEntity.setType("103");
        List<HomeMappingEntity> incinerationCount = mainIndexMapperDao.getDeviceCount(homeRequestEntity);
        homeRequestEntity.setType("104");
        List<HomeMappingEntity> kitchenCount = mainIndexMapperDao.getDeviceCount(homeRequestEntity);
        resultMap.put("incineration", incinerationCount);
        resultMap.put("kitchen", kitchenCount);
        return resultMap;
    }

    /**
     * gis展示
     */
    @Override
    public HomeResponseEntity getGisAnalysis(HomeRequestEntity homeRequestEntity) {
        String nowYear = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy"));
        String lastYear = LocalDate.now().minusYears(1).format(DateTimeFormatter.ofPattern("yyyy"));
        int nowDays = LocalDate.now().lengthOfYear();//获取今年天数
        int lastDays = LocalDate.now().minusYears(1).lengthOfYear();//获取去年天数
        homeRequestEntity.setStartTime(nowYear);
        HomeResponseEntity responseEntity = new HomeResponseEntity();
        //获取今年数据
        HomeMappingEntity nowGis = mainIndexMapperDao.gis(homeRequestEntity);
        //获取去年数据
        homeRequestEntity.setStartTime(lastYear);
        HomeMappingEntity lastGis = mainIndexMapperDao.gis(homeRequestEntity);
        int growthRate = 0;
        int resourceRate = 0;
        int harmlessRate = 0;
        if (nowGis == null && lastGis != null) {
            responseEntity.setResourceRate(0);
            if (lastGis.getTotal() != null && lastGis.getTotal().intValue() != 0) {
                growthRate = (new BigDecimal(lastGis.getTotal() != null ? -lastGis.getTotal() : 0)).divide(new BigDecimal(lastGis.getTotal()), 2, BigDecimal.ROUND_HALF_DOWN).multiply(new BigDecimal(100)).intValue();
            }
            responseEntity.setHarmlessRate(0);
            responseEntity.setReductionRate(growthRate);
            return responseEntity;
        } else {
            if (nowGis != null) {
                //计算累计资源化利用率
                //获取服务总人口
                HomeMappingEntity yearIndex = null;
                BigDecimal garbageTotal=null;
                homeRequestEntity.setTime(nowYear);
                 yearIndex = mainIndexMapperDao.getYearIndex(homeRequestEntity);
                if (yearIndex != null) {
                    //计算今年垃圾产生量
                     garbageTotal = new BigDecimal(yearIndex.getPersonTotal()).multiply(new BigDecimal(11 * nowDays));
                }else {
                    homeRequestEntity.setTime(lastYear);
                    yearIndex= mainIndexMapperDao.getYearIndex(homeRequestEntity);
                    //计算去年垃圾产生量
                    garbageTotal= new BigDecimal(yearIndex.getPersonTotal()).multiply(new BigDecimal(11 * lastDays));
                }

                int value = (garbageTotal.subtract(new BigDecimal(nowGis.getDisposedTotal() != null ? nowGis.getDisposedTotal() : 0))).intValue();
                if (value != 0) {
                    resourceRate = new BigDecimal(nowGis.getResourceUseRate() != null ? nowGis.getResourceUseRate() : 0).divide(new BigDecimal(value), 2, BigDecimal.ROUND_HALF_DOWN).multiply(new BigDecimal(100)).intValue();
                }
                //计算无害化处理率
                int result = garbageTotal.subtract(new BigDecimal(nowGis.getRecycleTotal()!=null?nowGis.getRecycleTotal():0)).intValue();
                if (result!= 0) {
                    harmlessRate = new BigDecimal(nowGis.getHarmLessRate() != null ? nowGis.getHarmLessRate() : 0).divide(new BigDecimal(result), 2, BigDecimal.ROUND_HALF_DOWN).multiply(new BigDecimal(100)).intValue();
                }
            }

            if (nowGis != null && lastGis == null) {
                growthRate = 100;
            }
            if (nowGis == null && lastGis == null) {
                growthRate = 0;
            }
            if (nowGis != null && lastGis != null) {
                //计算增长率
                if (lastGis.getTotal() != null && lastGis.getTotal().intValue() != 0) {
                    growthRate = (new BigDecimal(nowGis.getTotal() != null ? nowGis.getTotal() : 0).subtract(new BigDecimal(lastGis.getTotal() != null ? lastGis.getTotal() : 0))).divide(new BigDecimal(lastGis.getTotal()), 2, BigDecimal.ROUND_HALF_DOWN).multiply(new BigDecimal(100)).intValue();
                }
            }
            responseEntity.setReductionRate(growthRate);
            responseEntity.setResourceRate(resourceRate);
            responseEntity.setHarmlessRate(harmlessRate);
            return responseEntity;
        }

    }
}
